package com.microservice.salmon.service.brand;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.microservice.salmon.db.entities.Brand;
import com.microservice.salmon.db.entities.BrandCategory;
import com.microservice.salmon.db.entities.GoodsCategory;
import com.microservice.salmon.db.mapper.BrandMapper;
import com.microservice.salmon.service.exception.ResourceException;
import com.microservice.salmon.service.vo.BrandVO;
import com.salmon.common.ApiResponse;
import com.salmon.common.page.PageResult;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 品牌管理服务类
 * </p>
 *
 * @author 王洪庆 2019-01-15 22:18
 */
@Service
@Transactional(readOnly = true, propagation = Propagation.SUPPORTS)
public class BrandServiceImpl implements BrandService {

    @Autowired
    private BrandMapper brandMapper;

    @Override
    @Transactional
    public void add(BrandVO brandVO) {
        Brand brand = new Brand();
        BeanUtils.copyProperties(brandVO, brand);
        brand.setBrandId(null);
        int count = brandMapper.insertSelective(brand);
        if(count != 1) {
            throw new ResourceException(ApiResponse.Status.ADD_BRAND_ERROR);
        }
        batchInsertCategory(brandVO.getCatIds(), brand.getBrandId());
    }

    @Override
    @Transactional
    public int delete(Integer brandId) {
        Brand brand = new Brand();
        brand.setDataFlag(false);
        brand.setBrandId(brandId);
        return brandMapper.updateByPrimaryKeySelective(brand);
    }

    @Override
    @Transactional
    public void update(BrandVO brandVO) {
        if(brandVO.getBrandId() == null)
            throw new ResourceException(ApiResponse.Status.NO_PRIMARY_KEY);
        Brand brand = new Brand();
        BeanUtils.copyProperties(brandVO, brand);
        int count = brandMapper.updateByPrimaryKeySelective(brand);
        if(count != 1) {
            throw new ResourceException(ApiResponse.Status.UPDATE_ERROR);
        }
        //删除
        brandMapper.deleteCategoryBrand(brandVO.getBrandId());
        //插入新值
        batchInsertCategory(brandVO.getCatIds(), brand.getBrandId());
    }

    private void batchInsertCategory(List<Integer> catids, Integer brandId) {
        if(CollectionUtils.isEmpty(catids))
            return;
        List<BrandCategory> brandCategoryList = new ArrayList<>();
        catids.forEach(cid -> brandCategoryList.add(new BrandCategory(brandId, cid)));
        int count = brandMapper.insertCategoryBrand(brandCategoryList);
        if(count <= 0 )
            throw new ResourceException(ApiResponse.Status.ADD_BRAND_CATEGORY_ERROR);
    }

    public Brand getBrandInfoById(Integer brandId) {
        Brand brand = brandMapper.selectBrandInfoByBrandId(brandId);
        if(brand == null)
            throw new ResourceException(ApiResponse.Status.NOT_FOUND);
        return brand;
    }

    @Override
    public List<Brand> obtainBrandByCategoryId(Integer categoryId) {
        List<Brand> list = brandMapper.selectBrandInfoByCategoryId(categoryId);
        if(CollectionUtils.isEmpty(list))
            throw new ResourceException(ApiResponse.Status.NOT_FOUND);
        return list;
    }

    /**
     * 获取品牌信息
     * @return
     */
    @Override
    public PageResult<Brand> obtainBrandList(BrandVO brandVO) {
        return obtainGoodsInfoByBrandName(brandVO);
    }

    /**
     * 根据条件获取品牌信息
     */
    private PageResult<Brand> obtainGoodsInfoByBrandName(BrandVO brandVO) {
        PageHelper.startPage(brandVO.getPageNum(), brandVO.getPageSize());
        Example example = new Example(Brand.class);
        example.excludeProperties("createTime", "updateTime");
        example.orderBy("sortNo").asc();
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("dataFlag", Boolean.TRUE); // 未删除的数据
        if(brandVO.getBrandName() != null)
            criteria.andEqualTo("brandName", brandVO.getBrandName());
        if(brandVO.getBrandId() != null)
            criteria.andEqualTo("brandId", brandVO.getBrandId());
        List<Brand> brandList = brandMapper.selectByExample(example);
        PageInfo<Brand> pageInfo = new PageInfo<>(brandList);
        PageResult<Brand> pageResult = new PageResult<>();
        BeanUtils.copyProperties(pageInfo, pageResult);
        pageResult.setRows(brandList);
        return pageResult;
    }
}
